package com.dnjn.happiness.survey.surveyHandler;/*
package com.dnjn.happiness.survey.surveyHandler;

import cn.hutool.core.date.DateUtil;
import com.dnjn.happiness.survey.domain.Survey;
import com.dnjn.happiness.survey.service.ISurveyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Random;

@Lazy(false)
@Component
@EnableScheduling
public class SpringDynamicCornTask implements SchedulingConfigurer {

    private static final Logger logger = LoggerFactory.getLogger(SpringDynamicCornTask.class);

    private static final String READY = "READY";
    private static final String PROCESSING = "PROCESSING";
    private static final String FINISHED = "FINISHED";

    private Integer flag = 1;


    private static final String DEFAULT_CRON = "0 0/5 * * * ?";
//    private static final String DEFAULT_CRON = "0/5 * * * * ?";
    private String cron = DEFAULT_CRON;

    @Autowired
    private ISurveyService surveyService;


    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(() -> {
            logger.info("真正的任务执行逻辑。。。");
            System.out.println( DateUtil.now() + "开始执行============");
            String str = "0/5";
            List<Survey> readList = surveyService.lambdaQuery().eq(Survey::getDeleted, "0").eq(Survey::getSurveyStatus, READY).le(Survey::getDateStart, DateUtil.now()).list();
            if (!CollectionUtils.isEmpty(readList)){
                flag = 1;
                //更新
                for (Survey survey : readList) {
                    surveyService.lambdaUpdate().eq(Survey::getSurveyId,survey.getSurveyId()).set(Survey::getSurveyStatus,PROCESSING).update();
                }
            }
            List<Survey> processingList = surveyService.lambdaQuery().eq(Survey::getDeleted, "0").eq(Survey::getSurveyStatus, PROCESSING).le(Survey::getDateEnd, DateUtil.now()).list();
            if (!CollectionUtils.isEmpty(processingList)){
                flag = 1;
                //更新
                for (Survey survey : processingList) {
                    surveyService.lambdaUpdate().eq(Survey::getSurveyId,survey.getSurveyId()).set(Survey::getSurveyStatus,FINISHED).update();
                }
            }
            if (CollectionUtils.isEmpty(readList) && CollectionUtils.isEmpty(processingList)){
                //更新
                flag ++;
            }
            if (flag >= 12){
                flag = 12;
                str = "0/59";
            }else {
                str = "0/" + (flag * 5);
            }
            cron = "0 " + str + " * * * ?";
//            cron = str + " * * * * ?";
            setCron(cron);
            System.out.println("flag =====" + flag+ "====" + cron);
            System.out.println(DateUtil.now() + "结束 ------------------");
        }, triggerContext -> {
            return new CronTrigger(this.cron).nextExecutionTime(triggerContext);
//            CronTrigger trigger = new CronTrigger(cron);
//            return trigger.nextExecutionTime(triggerContext);
        });
    }
    public void setCron(String cron) {
        System.out.println("原来的cron："+this.cron+"更新后的cron："+cron);
        this.cron = cron;
    }
    public String getCron() {
        return this.cron;
    }


}
*/
